// const base64 = require('../utils/base64.js').Base64
import config from './config';
const global = {
	data() {
		return {
			limit: 20,
			theme_color: uni.getStorageSync('theme_color') || '#de3838',
			my_style: `--color:${uni.getStorageSync('theme_color') || '#de3838'};`,
			custom_btn: {
				backgroundColor: uni.getStorageSync('theme_color') || '#de3838',
			},
			common_href: 'https://yr-baseline.oss-cn-hangzhou.aliyuncs.com',
		}
	},
	methods: {
		// this.$forceUpdate()
		getTestToken(e) {
			uni.request({
				url: config + 'luckorder/checkPoorLock',
				data: {
					user_id: e
				},
				method: 'POST',
				header: {
					'content-type': 'application/json'
				},
				success: (res) => {
					this.poorlock = item.data.result
					let nowtime = new Date().getTime() + ''
					let sss = (item.data.result.un_look_time * 1000 - nowtime) / 1000 + ''
					this.poorlock.un_look_time = sss.split('.')[0]
				}
			});
		
		},
		
		openIng() {
			console.log(111111111155555555553333333333)
			// this.$u.toast('正在开发中')
		},
		/**
		 * 跳转
		 * @param {Object} url
		 */
		async jump(url) {
			if(url.split('/')){
				if(url.split('/')[1] == 'pages'){
					uni.switchTab({
						url,
					})
				}else{
					uni.navigateTo({
						url,
					})
				}
			}else{
				uni.navigateTo({
					url,
				})
			}
		},
		/**
		 * 返回上一页
		 */
		goBack(number) {
			uni.navigateBack(number || 1)
			// let pages = getCurrentPages(); // 当前页面
			// let beforePage = pages[pages.length - number+1 || 2]; // 上一页
			// uni.navigateBack({
			//     success: function() {
			//         beforePage.onLoad(); // 执行上一页的onLoad方法
			//     }
			// });
		},
		replaceJump(url) {
			uni.redirectTo({
				url,
			})
		},
		reLaunch(url) {
			uni.reLaunch({
				url,
			})
		},
		switchTab(url) {
			uni.switchTab({
				url,
			})
		},
		// 图片预览
		previewImage(item, list) {
			uni.previewImage({
				current: item,
				urls: list,
			})
		},
		/**
		 * 操作成功并返回
		 * @param {Object} msg
		 */
		successBack(msg, num) {
			uni.showToast({
				title: msg,
				icon: 'success',
			})
			setTimeout(() => {
				this.goBack(num)
			}, 1000)
		},
		showMsg(msg, icon) {
			uni.showToast({
				icon: icon || 'none',
				title: msg,
			})
		},
		// 获取当前时间
		dateTimeStr(str) {
			var date = new Date(),
				year = date.getFullYear(), //年
				month = date.getMonth() + 1, //月
				day = date.getDate(), //日
				hour = date.getHours(), //时
				minute = date.getMinutes(), //分
				second = date.getSeconds(); //秒
			month >= 1 && month <= 9 ? (month = "0" + month) : "";
			day >= 0 && day <= 9 ? (day = "0" + day) : "";
			hour >= 0 && hour <= 9 ? (hour = "0" + hour) : "";
			minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
			second >= 0 && second <= 9 ? (second = "0" + second) : "";
			if (str.indexOf('y') != -1) {
				str = str.replace('y', year)
			}
			if (str.indexOf('m') != -1) {
				str = str.replace('m', month)
			}
			if (str.indexOf('d') != -1) {
				str = str.replace('d', day)
			}
			if (str.indexOf('h') != -1) {
				str = str.replace('h', hour)
			}
			if (str.indexOf('i') != -1) {
				str = str.replace('i', minute)
			}
			if (str.indexOf('s') != -1) {
				str = str.replace('s', second)
			}
			return str;
		},
		// 成功
		successGo(info) {
			uni.redirectTo({
				url: `/views/user/success?info=${info}`,
			})
		},
		// 时间戳转换为标准时间
		getStepTime(nS) {
			var now = new Date(nS * 1000)
			var y = now.getFullYear()
			var m = now.getMonth() + 1
			var d = now.getDate()
			return (
				y +
				'-' +
				(m < 10 ? '0' + m : m) +
				'-' +
				(d < 10 ? '0' + d : d) +
				' ' +
				now.toTimeString().substr(0, 8)
			)
		},
		// 刷新
		refreshList(fn) {
			this.page = 1
			this.list = []
			this.status = 'loading'
			fn && fn()
		},
		// 获取主题色
		async getThemeColor() {
			const res = await this.$api.common.system('getThemeColor')
			if (res.code === 200) {
				const {
					color
				} = res.data
				uni.setStorageSync('theme_color', color)
				this.theme_color = uni.getStorageSync('theme_color') || '#de3838'
				this.my_style = `--color:${uni.getStorageSync('theme_color') || '#de3838'};`
				this.custom_btn = {
					backgroundColor: uni.getStorageSync('theme_color') || '#de3838',
				}
			}
		},
		/**
		 * 获取今天的时间或者指定时间戳
		 * @param date
		 * @returns {string}
		 */
		nowTime(date) {
			return (date ? date / 1000 : new Date().getTime() / 1000).toFixed(0)
		},
		// 去登录
		toLogin() {
			// #ifdef APP-PLUS || H5
			this.jump('/views/share/login')
			// #endif
			// #ifdef MP-WEIXIN
			this.jump('/views/auth/authorize')
			// #endif
		},
		toBind() {
			this.jump('/views/auth/bind_phone')
		},
		killTime(end_time) {
			let that = this
			let now_time = (new Date().getTime() / 1000).toFixed(0) // 当前时间
			// 剩余时间
			return +end_time - +now_time
		},
		dataString(date) {
			date = date.substring(0, 19)
			date = date.replace(/-/g, '/') // 必须把日期'-'转为'/'
			return new Date(date).getTime() / 1000
		},

		// 随机字符串
		randomStr(len) {
			len = len || 32
			var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789'
			var maxPos = chars.length
			var pwd = ''
			for (let i = 0; i < len; i++) {
				pwd += chars.charAt(Math.floor(Math.random() * maxPos))
			}
			return pwd
		},
		isListNone(list) {
			return JSON.stringify(list) !== '[]'
		},
		isObjNone(obj) {
			return JSON.stringify(obj) !== '{}'
		},
		stopClick() {},
		// 复制
		copyText(val) {
			uni.setClipboardData({
				data: val,
				success(res) {
					uni.getClipboardData({
						success(res) {
							uni.showToast({
								title: '复制成功',
							})
						},
					})
				},
			})
		},
		h5Copy(content) {
			if (!document.queryCommandSupported('copy')) {
				// 不支持
				return false
			}
			let textarea = document.createElement('textarea')
			textarea.value = content
			textarea.readOnly = 'readOnly'
			document.body.appendChild(textarea)
			textarea.select() // 选择对象
			textarea.setSelectionRange(0, content.length) // 核心
			let result = document.execCommand('copy') // 执行浏览器复制命令
			textarea.remove()
			return result
		},
		// 打电话
		callPhone(phone) {
			uni.makePhoneCall({
				phoneNumber: phone,
			})
		},
		// textarea字数
		descInput(e, max) {
			if (e.detail.value.length > max) {
				return
			}
			return e.detail.value.length
		},
		userIsLogin() {
			if (!uni.getStorageSync('member_id')) {
				this.toLogin()
				return false
			}
			return true
		},
		// 内链外链跳转
		customJump(item) {
			console.log(item)
			const {
				jump_type,
				jump_id,
				pages
			} = item
			let list = [10011, 10010]
			switch (Number(jump_type)) {
				// 自定义页面
				case 1:
					wx.navigateTo({
						url: `/views/home/custom?id=${jump_id}`,
					})
					break
					// 商品
				case 2:
					wx.navigateTo({
						url: `/views/goods/goods_detail?id=${jump_id}`,
					})
					break
					// 领券中心
				case 3:
					wx.navigateTo({
						url: `/views/home/coupon_center`,
					})
					break
					// 满减
				case 4:
					wx.navigateTo({
						url: `/views/home/special?activity_type=manjian&id=${jump_id}`,
					})
					break
					// 限时
				case 5:
					wx.navigateTo({
						url: `/views/home/special?activity_type=discount&id=${jump_id}`,
					})
					break
					// 第二
				case 6:
					wx.navigateTo({
						url: `/views/home/special?activity_type=secondhalf&id=${jump_id}`,
					})
					break
					// 阶梯
				case 7:
					wx.navigateTo({
						url: `/views/home/special?activity_type=jieti&id=${jump_id}`,
					})
					break
				case 9:
					if (!pages) {
						this.$u.toast('正在开发中')
					}

					if (list.indexOf(jump_id) > -1) {
						uni.reLaunch({
							url: pages,
						})
					} else {
						wx.navigateTo({
							url: pages,
						})
					}
					break
				default: {
					this.$u.toast('正在开发中')
				}
			}
		},
		// 时间戳判断周几
		getWeekDay(date, head) {
			head = head || '星期'
			let day = new Date(date).getDay()
			let weekday = ['日', '一', '二', '三', '四', '五', '六']
			return head + weekday[day]
		},
		// 时间戳转换年月日
		timestamp(str) {
			let date = new Date(str)
			return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate()
		}
		
	},
}
export default global
